package com.zhuhjay.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.ruiji.common.R;
import com.zhuhjay.ruiji.domain.Employee;
import com.zhuhjay.ruiji.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @author ZhuHJay
 * @since 2022-05-15
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    private final IEmployeeService employeeService;
    public EmployeeController(IEmployeeService iEmployeeService) {
        this.employeeService = iEmployeeService;
    }

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        // 明文密码使用 MD5 加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        // 查询用户数据
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        // 查不到就登陆失败
        if(emp == null){
            return R.error("登录失败");
        }
        // 比对密码是否正确
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        // 员工状态
        if(emp.getStatus() == 0){
            return R.error("账号禁用");
        }
        // 登录成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        // 设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 保存数据
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(@RequestParam(name = "page", defaultValue = "1", required = false) Integer page,
                                  @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                  @RequestParam(name = "name", required = false) String name){
        // 判断是否模糊查找用户, 如果模糊查找那么就分页到第一页, 否则会数据丢失显示
        if( StringUtils.isNotEmpty(name) ){
            page = 1;
        }
        Page<Employee> employeePage = new Page<>(page, pageSize);
        // 查询数据
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername, name)
                    .orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage, queryWrapper);
        return R.success(employeePage);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable(name = "id") Long id){
        return R.success(employeeService.getById(id));
    }

}
